package com.person.jiaiplan.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.jiaiplan.mapper.ClocklogsMapper;
import com.person.jiaiplan.pojo.dto.ClockInReqDTO;
import com.person.jiaiplan.pojo.entity.AchievementLogs;
import com.person.jiaiplan.pojo.entity.Clocklogs;
import com.person.jiaiplan.pojo.entity.ScheduleLogs;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.AchievementLogsService;
import com.person.jiaiplan.service.ClocklogsService;
import com.person.jiaiplan.service.ScheduleLogsService;
import com.person.jiaiplan.util.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Slf4j
@Service
public class ClocklogsServiceImpl extends ServiceImpl<ClocklogsMapper, Clocklogs> implements ClocklogsService {

    @Autowired
    private ScheduleLogsService scheduleLogsService;

    @Autowired
    private AchievementLogsService achievementLogsService;


    @Override
    public Boolean clockIn(ClockInReqDTO clockInReqDTO) {
        Date clockTime = clockInReqDTO.getClockTime();
        String scheduleId = clockInReqDTO.getScheduleId();
        MultipartFile file = clockInReqDTO.getFile();
        Double useageHour = clockInReqDTO.getUseageHour();
        String clockDate = DateUtil.formatDate(clockTime);
        String remark = clockInReqDTO.getRemark();
        Integer clockType = clockInReqDTO.getClockType();

        // 根据scheduleId 创建相应的文件夹
        ScheduleLogs scheduleLogs = scheduleLogsService.getById(scheduleId);
        String rootPath = UploadUtils.getRootPath(scheduleLogs, clockDate);
        // 上传文件
        UploadUtils.uploadFiles(rootPath, file);

        // 保存上传文件记录
        AchievementLogs achievementLogs = new AchievementLogs();
        achievementLogs.setId(SignleSnowFlake.getSnowflakeId());
        achievementLogs.setMkdir(rootPath);
        achievementLogs.setFileName(file.getOriginalFilename());
        achievementLogs.setScheduleId(scheduleId);
        achievementLogs.setCreateTime(new Date());
        achievementLogsService.saveOrUpdateAchievement(achievementLogs);

        // 保存打卡记录
        saveClockLogs(clockTime, scheduleId, useageHour, remark,clockType);


        return true;
    }

    @Override
    public Boolean saveClock(ClockInReqDTO req) {
        saveClockLogs(req.getClockTime(), req.getScheduleId(), req.getUseageHour(), req.getRemark(),req.getClockType());
        return true;
    }

    @Override
    public Boolean removeByScheduleId(Long ScheduleId) {
        return this.remove(Wrappers.<Clocklogs>lambdaQuery().eq(Clocklogs::getScheduleId, ScheduleId));
    }

    /**
     * 保存打卡记录
     *
     * @param clockTime
     * @param scheduleId
     */
    public Boolean saveClockLogs(Date clockTime, String scheduleId, Double useageHour, String remark,Integer clockType) {

        // 将clockTime 的时分秒设置为0
        clockTime = DateUtil.beginOfDay(clockTime);

        if ("0".equals(scheduleId)) {
            return false;
        }

        // 默认正常打卡
        if (clockType == null){
            clockType = 1;
        }

        // 存在打卡记录则只更新时间
        if (this.count(Wrappers.<Clocklogs>lambdaQuery()
                .eq(Clocklogs::getScheduleId, scheduleId)
                .eq(Clocklogs::getClockTime, clockTime)
                .eq(Clocklogs::getClockType,clockType)) >= 1) {
            Clocklogs clocklogs = this.getOne(Wrappers.<Clocklogs>lambdaQuery()
                    .eq(Clocklogs::getScheduleId, scheduleId)
                    .eq(Clocklogs::getClockTime, clockTime)
                    .eq(Clocklogs::getClockType, clockType));
            clocklogs.setUseageHour(useageHour);
            clocklogs.setRemark(remark);
            return this.updateById(clocklogs);
        }


        Clocklogs clocklogs = new Clocklogs();
        clocklogs.setId(SignleSnowFlake.getSnowflakeId());
        clocklogs.setClockTime(clockTime);
        clocklogs.setScheduleId(Long.valueOf(scheduleId));
        clocklogs.setCreateTime(DateUtil.date());
        clocklogs.setUseageHour(useageHour);
        clocklogs.setRemark(remark);
        clocklogs.setClockType(clockType);
        return this.save(clocklogs);
    }
}
